<!--#
layout("/layouts/platform.html"){
#-->
<style>
    .left_aside {
        width: 132px;
        height: 500px;
    }

    .left_tree {
        margin-top: 10px;
        margin-left: -5px;
    }

    /*当前分类标识颜色*/
    /*.left_tree .is-current .el-tree-node__content{*/
    /*    background-color: #F5F7FA;*/
    /*}*/

    .el-tree-node {
        padding-top: 7px;
    }

    .note {
        width: 100%;
        height: 320px;
    }

    .el-upload input[type=file] {
        display: none;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 128px;
        height: 128px;
        line-height: 128px;
        text-align: center;
    }

    .avatar {
        width: 128px;
        height: 128px;
        display: block;
    }
</style>
<div id="app" v-cloak>
    <el-row class="header navbar bg-white shadow">
        <div class="btn-group tool-button mt5">
            <el-input v-model="pageForm.searchKeyword" @keyup.enter.native="doSearch" placeholder="请输入群名或群主昵称、手机号、用户名"
                      size="medium"></el-input>
        </div>
        <!--搜索按钮-->
        <div class="btn-group tool-button mt5">
            <el-button slot="append" icon="el-icon-search" @click="doSearch" size="medium"></el-button>
        </div>
        <!--新增按钮-->
        <div class="pull-right offscreen-right mt5">
            <el-button size="medium" @click="openAdd"><i class="ti-plus"></i>添加群组</el-button>
        </div>
    </el-row>
    <el-row class="el-table-container">
        <el-table
                :data="tableData"
                size="small"
                header-align="center"
                style="width: 100%">
            <el-table-column
                    prop="groupHeadPicture"
                    label="群头像"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
                <template scope="scope">
                    <p v-if="scope.row.groupHeadPicture">
                        <img :src=scope.row.groupHeadPicture style="width: 60px;height: 60px">
                    </p>
                    <p v-else>
                        暂无数据
                    </p>
                </template>
            </el-table-column>
            <el-table-column
                    label="群名"
                    prop="groupName"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
            </el-table-column>
            <el-table-column
                    label="群主"
                    prop="nickname"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
            </el-table-column>

            <el-table-column
                    sortable
                    prop="ctAt"
                    label="时间"
                    header-align="center"
                    align="center">
                <template scope="scope">
                    {{formatAt(scope.row.ctAt)}}
                </template>
            </el-table-column>
            <!--操作下拉菜单-->
            <el-table-column
                    label="操作"
                    width="120"
                    align="center">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item divided :command="{type:'addMember', id:scope.row.id}">
                                添加新成员
                            </el-dropdown-item>
                            <el-dropdown-item divided :command="{type:'edit', id:scope.row.id, row:scope.row}">
                                修改
                            </el-dropdown-item>
                            <el-dropdown-item divided :command="{type:'del', id:scope.row.id}">
                                删除
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <!--分页容器-->
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>
    <!--新增/修改按钮弹出的dialog-->
    <el-dialog
            :title="titlePopu==0?'新增群组':'修改群组'"
            :visible.sync="editDialogVisible"
            :close-on-click-modal="false"
            width="40%">
        <el-form :model="formData" ref="editForm" :rules="formRules" size="small" label-width="80px">
            <!--新增/修改群名称input-->
            <el-form-item prop="groupName" label="群名">
                <el-input placeholder="请输入群名"
                          v-model="formData.groupName"
                          auto-complete="off"
                          tabindex="1"
                          type="text"
                          style="width: 215px">
                </el-input>
            </el-form-item>
            <!--新增/修改群主input-->
            <el-form-item prop="masterId" label="群主">
                <el-select
                        v-model="formData.masterId"
                        filterable
                        remote
                        reserve-keyword
                        placeholder="请输入用户手机号或昵称或用户名"
                        :remote-method="getUsers"
                        :loading="loading">
                    <el-option
                            v-for="item in userOptions"
                            :key="item.id"
                            :label="item.label"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item prop="groupHeadPicture" label="群头像">
                <el-upload
                        class="avatar-uploader"
                        tabindex="5"
                        action="${base}/open/file/upload/image2"
                        name="Filedata"
                        :show-file-list="false"
                        :before-upload="beforeUpload"
                        :on-success="function(resp,file,fileList){return handlePicSuccess(resp,file,fileList,'groupHeadPicture')}">
                    <img v-if="formData.groupHeadPicture" :src="formData.groupHeadPicture" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
        </el-form>
        <!--        确认/取消 提交修改按钮-->
        <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="doEdit">确 定</el-button>
        </span>
    </el-dialog>

    <el-dialog
            title="选择用户"
            :visible.sync="selectDialogVisible"
            width="65%">
        <div class="btn-group tool-button mt5">
            <el-input placeholder="请输入用户名或昵称或手机号" v-model="selectPageForm.searchKeyword" @keyup.enter.native="doSearch">
                <el-button slot="append" icon="el-icon-search" @click="doSelectSearch"></el-button>
            </el-input>
        </div>
        <el-table
                :data="selectTableData"
                @sort-change='selectPageOrder'
                header-align="center"
                style="width: 100%;margin-top: 10px;margin-bottom: 10px;"
                :default-sort="{prop: 'opAt', order: 'descending'}"
                @selection-change="handleSelectionChange"
                size="mini">
            <el-table-column
                    type="selection"
                    width="35">
            </el-table-column>

            <el-table-column
                    prop="headPicture"
                    label="头像"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
                <template scope="scope">
                    <p v-if="scope.row.headPicture && scope.row.headPicture.substring(0, 1) == '/'">
                        <img :src="scope.row.headPicture" style="width: 60px;height: 60px">
                    </p>
                    <p v-else-if="scope.row.headPicture && scope.row.headPicture.substring(0, 1) != '/'">
                        <img :src="'/' + scope.row.headPicture" style="width: 60px;height: 60px">
                    </p>
                    <p v-else>
                        暂无数据
                    </p>
                </template>
            </el-table-column>
            <el-table-column
                    prop="userName"
                    label="用户名"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="nickName"
                    label="用户昵称">
            </el-table-column>
            <el-table-column
                    prop="phone"
                    label="手机号">
            </el-table-column>
        </el-table>
        <el-pagination
                @size-change="selectPageSizeChange"
                @current-change="selectPageNumberChange"
                :current-page="selectPageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="selectPageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="selectPageForm.totalCount">
        </el-pagination>
        <span slot="footer" class="dialog-footer">
            <el-button @click="selectDialogVisible = false"> 取 消 </el-button>
            <el-button type="primary" @click="doAddAll"> 确 定 </el-button>
        </span>
    </el-dialog>
</div>
<script>
    new Vue({
        el: "#app",
        data: function () {
            return {
                userOptions: [],
                searchDate: [],
                addDialogVisible: false,
                editDialogVisible: false,
                selectDialogVisible: false,
                titlePopu: 0,
                tableData: [],
                selectUsers: [],
                selectTableData: [],
                selectPageForm: {
                    groupId: "",
                    searchName: "",
                    searchKeyword: "",
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0
                },
                pageForm: {
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                    userType: "",
                    searchKeyword: "",
                    searchDate: "",
                },
                formData: {
                    id: "",
                    groupHeadPicture: "",
                    groupName: "",
                    masterId: "",
                },
                formRules: {
                    groupName: [
                        {required: true, message: '请输入群名', trigger: 'blur'}
                    ],
                    masterId: [
                        {required: true, message: '请选择群主', trigger: 'blur'}
                    ],
                    groupHeadPicture: [
                        {required: true, message: '请选择头像', trigger: 'blur'}
                    ]
                },
                loading: false,
                fileType: ['jpg', 'png', 'jpeg', 'gif']
            }
        },
        methods: {
            formatAt: function (val) {
                if (val > 0)
                    return moment(val * 1000).format("YYYY-MM-DD HH:mm");
                return "";
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                var self = this;
                sublime.showLoadingbar();//显示loading
                if (self.searchDate) {
                    self.pageForm.searchDate = self.searchDate.toString();
                } else {
                    self.pageForm.searchDate = "";
                }
                $.post(base + "/platform/user/group/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code === 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            doSearch: function () {
                this.pageForm.pageNumber = 1;
                this.pageData();
            },
            openAdd: function () {
                let self = this;
                self.titlePopu = 0;
                self.editDialogVisible = true;
                if (self.$refs["editForm"]) {
                    self.$refs["editForm"].resetFields();
                }
                self.formData = {};
            },
            dropdownCommand: function (command) {//监听下拉框事件
                let self = this;
                if ("edit" === command.type) {
                    self.editDialogVisible = true;
                    if (this.$refs["editForm"]) {
                        this.$refs["editForm"].resetFields();
                    }
                    //user回显
                    self.userOptions = [{id: command.row.masterId, label: command.row.nickname}];
                    self.formData = {
                        id: command.row.id,
                        groupHeadPicture: command.row.groupHeadPicture,
                        groupName: command.row.groupName,
                        masterId: command.row.masterId
                    };
                    self.titlePopu = 1;
                }
                if ("del" === command.type) {
                    self.$confirm('您确定要删除这条数据？ ', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        callback: function (a, b) {
                            if ("confirm" === a) {//确认后再执行
                                $.post(base + "/platform/user/group/del", {id: command.id}, function (data) {
                                    if (data.code === 0) {
                                        self.$message({
                                            message: data.msg,
                                            type: 'success'
                                        });
                                        self.pageData();
                                    } else {
                                        self.$message({
                                            message: data.msg,
                                            type: 'error'
                                        });
                                    }
                                }, "json");
                            }
                        }
                    });
                }
                if ("addMember" === command.type) {
                    self.selectDialogVisible = true;
                    self.selectTableData = [];
                    self.selectPageForm.groupId = command.id;
                    self.selectPageData();
                }
            },
            doEdit: function () {
                let self = this;
                self.$refs["editForm"].validate(function (valid) {
                    let url = "";
                    if (valid) {
                        //titlePopu=0是新增,1是修改
                        if (self.titlePopu === 0) {
                            url = "/platform/user/group/add";
                        } else {
                            url = "/platform/user/group/edit";
                        }
                        $.post(base + url, self.formData, function (data) {
                            if (data.code === 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.doSearch();
                                self.editDialogVisible = false;
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            getUsers: function (key) {
                let self = this;
                $.post(base + "/platform/user/manager/getUsers", {key: key}, function (data) {
                    self.userOptions = data.code === 0 ? data.data.list.map(ite => {
                            ite.label = ite.nickName + '#' + ite.phone + '$' + ite.userName;
                            return ite
                        }
                    ) : [];
                }, "json");
            },
            handlePicSuccess: function (response, file, fileList, attrName) {// 上传图片
                if (response.code === 0) {
                    // 对象属性直接赋值不会触发试图更新，采用 Vue.set 、this.$set或者Object.assign({}，this.obj)创建新对象
                    // 或者也可以定义一个 refresh 属性 ，当数据发生改变时，该属性同时变化，也会触发视图更新
                    // this.formData.site_logo =response.data;
                    this.$set(this.formData, attrName, response.data);
                } else {
                    this.$set(this.formData, attrName, "");
                }
            },
            beforeUpload: function (file) {
                let isLt120M = file.size / 1024 / 1024 < 200;
                if (this.fileType.indexOf(file.name.substring(file.name.lastIndexOf(".") + 1, file.name.length)) < 0) {
                    this.$message.error('文件格式不符，仅可以上传' + this.fileType.join(','));
                    return false;
                }
                if (!isLt120M) {
                    this.$message.error('文件大小不能超过200MB');
                    return false;
                }
                return true;
            },
            doSelectSearch: function () {
                this.selectPageForm.pageNumber = 1;
                this.selectPageData();
            },
            handleSelectionChange: function (val) {
                this.selectUsers = val;
            },
            selectPageOrder: function (column) {//按字段排序
                this.selectPageForm.pageOrderName = column.prop;
                this.selectPageForm.pageOrderBy = column.order;
                this.selectPageData();
            },
            selectPageNumberChange: function (val) {//页码更新操作
                this.selectPageForm.pageNumber = val;
                this.selectPageData();
            },
            selectPageSizeChange: function (val) {//分页大小更新操作
                this.selectPageForm.pageSize = val;
                this.selectPageData();
            },
            selectPageData: function () {//加载分页数据
                let self = this;
                $.post(base + "/platform/user/group/memberList", self.selectPageForm, function (data) {
                    if (data.code === 0) {
                        self.selectTableData = data.data.list;
                        self.selectPageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            doAddAll: function () {
                let self = this;
                let ids = [];
                self.selectUsers.forEach(function (val) {
                    ids.push(val.id);
                });
                self.$confirm('您确定要选中的 ' + ids.length + ' 位用户添加入群？ ', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    callback: function (a, b) {
                        if ("confirm" === a) {//确认后再执行
                            $.post(base + "/platform/user/group/batchAdd", {
                                ids: ids.toString(),
                                groupId: self.selectPageForm.groupId
                            }, function (data) {
                                if (data.code === 0) {
                                    self.$message({
                                        message: data.msg,
                                        type: 'success'
                                    });
                                    self.selectDialogVisible = false;
                                    self.pageData();
                                } else {
                                    self.$message({
                                        message: data.msg,
                                        type: 'error'
                                    });
                                }
                            }, "json");
                        }
                    }
                });
            },
        },
        created: function () {
            this.pageData();
        }
    });
</script>

<!--#
}
#-->